import asyncio
from util import delay, async_timed
from PyCmpltrtok.common import sep
import time


@async_timed()
async def main_task():
    return await delay(2, 'main_delay', interval=0.2)


def main():
    loop = asyncio.new_event_loop()
    
    try:
        sep('Will start task')
        task = loop.create_task(main_task())
        
        """
        Different from asyncio.create_task that starts the task immediately.
        Because the manually created loop has not been started yet.
        """
        
        print('sleep 1')
        time.sleep(1)
        sep('Will loop.run_until_complete')
        loop.run_until_complete(task)
    finally:
        sep('Close loop')
        loop.close()
        print('Loop closed')


if '__main__' == __name__:
    
    main()
    